package com.fangzhi.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fangzhi.mapper.ProductTechnologyMapper;
import com.fangzhi.mapper.ProductTypeMapper;
import com.fangzhi.mobel.OrderProducts;
import com.fangzhi.mobel.ProductTechnology;
import com.fangzhi.mobel.ProductType;
import com.fangzhi.mobel.io.SaveOrderIo;
import com.fangzhi.service.OrderProductsService;
import com.fangzhi.mapper.OrderProductsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author edy
* @description 针对表【order_products】的数据库操作Service实现
* @createDate 2024-09-18 14:12:43
*/
@Service
public class OrderProductsServiceImpl extends ServiceImpl<OrderProductsMapper, OrderProducts>
    implements OrderProductsService{

    @Autowired
    private ProductTypeMapper productTypeMapper;
    @Autowired
    private ProductTechnologyMapper productTechnologyMapper;

    @Override
    public Integer saveOrderDetail(String orderId, List<SaveOrderIo.Product> productList) {

        List<OrderProducts> orderProducts = productList.stream().map(T -> {
            ProductType productType = productTypeMapper.selectById(T.getProductTypeId());

            int sum = 0;
            List<Long> collect = new ArrayList<>();
            if (!T.getTechnologyId().isEmpty()){
                List<ProductTechnology> productTechnologies = productTechnologyMapper.selectList(Wrappers.<ProductTechnology>lambdaQuery()
                        .in(ProductTechnology::getId, T.getTechnologyId()));
                if (productTechnologies.isEmpty() || productTechnologies.size() != T.getTechnologyId().size())
                    throw new RuntimeException("商品不存在");
                sum = productTechnologies.stream().mapToInt(ProductTechnology::getPrice).sum();
                collect = productTechnologies.stream().map(ProductTechnology::getId).collect(Collectors.toList());
            }
            int premiumPrice = T.getPremiumPrice().multiply(BigDecimal.valueOf(100)).intValue();
            OrderProducts o = new OrderProducts().setOrderId(orderId).setProductTypeId(T.getProductTypeId()).setNum(T.getNum())
                    .setTotalAmount((productType.getPrice() + sum + premiumPrice) * T.getNum())
                    .setComments(T.getComments()).setTechnologyId(JSON.toJSONString(collect)).setPrice(productType.getPrice())
                    .setPremiumPrice(premiumPrice).setBadTypeId(T.getBadTypeId());
            Long productTypeId = T.getProductTypeId();
            do {
                ProductType productType1 = productTypeMapper.selectById(productTypeId);
                if (productType1.getLevel() == 3){
                    o.setThree(productType1.getProductName());
                }else if(productType1.getLevel() == 2){
                    o.setTwo(productType1.getProductName());
                }else if(productType1.getLevel() == 1){
                    o.setOne(productType1.getProductName());
                }
                productTypeId = productType1.getParentId();
            }while (productTypeId != 0);
            return o;
        }).collect(Collectors.toList());
        boolean b = this.saveBatch(orderProducts);
        if (b)
            return  orderProducts.stream().mapToInt(OrderProducts::getTotalAmount).sum();
        throw new RuntimeException("添加订单产品失败");
    }

    @Override
    public List<OrderProducts> queryListByType(int i, String orderId) {
        return this.getBaseMapper().queryListByType(i, orderId);
    }
}




